package la.iok.hzsvn.lewin.movie.cloud.controller;

import la.iok.hzsvn.lewin.movie.cloud.model.RoomParam;
import la.iok.hzsvn.lewin.movie.cloud.service.DashBoardService;
import la.iok.hzsvn.lewin.movie.cloud.service.RoomService;
import la.iok.hzsvn.lewin.movie.core.Token;
import la.iok.hzsvn.lewin.movie.core.model.RoomVo;
import la.iok.hzsvn.lewin.mybatis.model.PageVo;
import la.iok.hzsvn.share.response.OperatorResponse;
import la.iok.hzsvn.share.response.OperatorResponses;
import la.iok.hzsvn.share.response.SingleDataResponse;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("rooms")
public class RoomController {
    private final RoomService roomService;
    private final DashBoardService dashBoardService;

    public RoomController(RoomService roomService,
                          DashBoardService dashBoardService) {
        this.roomService = roomService;
        this.dashBoardService = dashBoardService;
    }

    @PostMapping
    public OperatorResponse add(@RequestHeader Token token, @RequestBody RoomParam param){
        roomService.save(token,param);
        return OperatorResponses.success();
    }

    @DeleteMapping("{id}")
    public OperatorResponse delete(@RequestHeader Token token, @PathVariable Long id){
        roomService.delete(id,token);
        return OperatorResponses.success();
    }

    @PutMapping("{id}")
    public OperatorResponse modify(@RequestHeader Token token, @PathVariable Long id, @RequestBody RoomParam param){
        roomService.update(token,id,param);
        return OperatorResponses.success();
    }

    @GetMapping
    public SingleDataResponse<PageVo<RoomVo>> page(@RequestHeader Token token, int page, int limit, Long orgUnitId, String name){
        page = Math.max(1,page);
        limit = Math.min(Math.max(1,limit),50);
        PageVo<RoomVo> pageRecords = roomService.page(page,limit,token,orgUnitId,name);
        return new SingleDataResponse<>(pageRecords);
    }
    @GetMapping("total")
    public SingleDataResponse<Integer> total(@RequestHeader Token token, Long orgUnitId){
        return new SingleDataResponse<>(dashBoardService.roomTotalCount(token,orgUnitId));
    }
}
